---
title: 'docker 容器'
date: '2023-08-05'
lastmod: '2023-12-05'
tags: ['docker']
# draft: false
summary: '容器虚拟化技术'
# images: ['/static/images/twitter-card.png']
---


- [docker-install](https://docs.docker.com/engine/install)   


<br/>    

**Debian安装**
```shell
# 先设置安装 docker 需要的 GPG key
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

# 安装docker
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

systemctl enable docker
systemctl start docker
```


**centos 安装**

1. 安装所需依赖
   
   1. yum install -y yum-utils device-mapper-persistent-data lvm2

2. 添加 docker repo源
   
   1. 使用阿里云镜像 repo 源(国内网络环境推荐阿里云)
      yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
   
   2. 或使用官方 repo 源
      yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

3. `yum install -y docker-ce`

4. `docker -v`    确认查看版本

5. `systemctl enable docker`
   


阿里镜像加速

![](https://pic4.zhimg.com/80/v2-5edf67a347611a21ca56fc8272fa9853_1440w.webp)
```shell
    sudo mkdir -p /etc/docker
    sudo tee /etc/docker/daemon.json <<-'EOF'
    {
      "registry-mirrors": ["https://zjj1c410.mirror.aliyuncs.com"]
    }
    EOF
    sudo systemctl daemon-reload
    sudo systemctl restart docker
```
#### 开启主机报文转发 

不开启会导致docker容器里面访问不到外网

```
cat > /etc/sysctl.d/docker.conf <<EOF

net.ipv4.ip_forward     =1

EOF && sysctl -p
```



#### 开启2375管理端口    

外网服务器请勿开启 会导致 docker 服务器未授权访问
```
vim /usr/lib/systemd/system/docker.service
#修改ExecStart 处为
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
```


#### 安装验证

```shell
    # 设置开机自启
    sudo systemctl start docker
    sudo systemctl enable docker  
    # 验证
    docker -v 或 docker version 或 docker --version
    # 或
    docker info

    # 1.先获取文件镜像位置 hello-world 为默认镜像
    docker pull library/hello-world
    # docker pull images 是抓取 image 文件，library/hello-world 是 image 文件在仓库里面的位置，其中 library 是 image 文件所在的组，hello-world 是 image 文件的名字。

    # 2.查看文件
    docker images
    查看到 本机下所有镜像 

    # 3.运行文件
    docker run  hello-world
```

#### 镜像源设置

```shell
vim /etc/docker/daemon.json
{
    "registry-mirrors": [
        "https://jmavzoni.mirror.aliyuncs.com",
        "https://www.daocloud.io/mirror",
        "http://hub-mirror.c.163.com/",
        "https://dockerhub.azk8s.cn/",   
        "https://registry.docker-cn.com/",
        "https://docker.mirrors.ustc.edu.cn/",
        "https://docker.m.daocloud.io/",
        "https://noohub.ru/",
        "https://huecker.io/",
    ]
}
systemctl daemon-reload
systemctl restart docker

```